package com.xxd.gulimail.product.controller;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xxd.gulimail.product.entity.CategoryEntity;
import com.xxd.gulimail.product.service.CategoryService;
import com.xxd.common.utils.PageUtils;
import com.xxd.common.utils.R;


/**
 * 商品三级分类
 *
 * @author xxd
 * @email 2132862687@qq.com
 * @date 2022-09-26 10:32:57
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查出所有分类以及子分类，以树形结构组装起来
     */
    @RequestMapping("/list/tree")
    public R list() {
        List<CategoryEntity> entityList = categoryService.listWithTree();
        return R.ok().put("data", entityList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);
        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category) {
        if (categoryService.save(category)) {
            return R.ok();
        } else {
            return R.error("出错啦");
        }

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateCascade(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update/status")
    public R updateStatus(@RequestBody CategoryEntity category) {
        categoryService.updateById(category);
        return R.ok();
    }
    /**
     * 拖拽修改,将拖拽的目标子节点的层级批量修改
     * 1.获得所有的子节点（parentCid == catId,遍历）
     */
    @RequestMapping("/update/drop")
    public R updateDrop(@RequestBody CategoryEntity category) {
        Long catId = category.getCatId();
        int level = category.getCatLevel();
        if (categoryService.getById(catId).getCatLevel().equals(level)) {//若修改后的层级与原来的一样，则只需更改parentId
            categoryService.updateById(category);
        } else {//递归子菜单修改层级
            List<CategoryEntity> allChildren = categoryService.getAllChildrenById(catId);
            if (allChildren.size() > 0) {
                for (CategoryEntity allChild : allChildren) {
                    allChild.setCatLevel(level + 1);
                    categoryService.updateById(allChild);
                    if (allChild.getChildren().size() > 0) {
                        for (CategoryEntity child : allChild.getChildren()) {
                            allChild.setCatLevel(level + 2);
                            categoryService.updateById(child);
                        }
                    }
                }
            }
        }
        return R.ok();
    }


    /**
     * 删除
     *
     * @RequestBody:获取请求体，获取前端的json数据
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds) {
//        categoryService.removeByIds(Arrays.asList(catIds));
//      1.检查当前删除的菜单是否被别的地方引用
        categoryService.removeMenuByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
